package com.game.autoTool.project.web.common;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.*;

public class SwingComponentsDemo {
    public static void main(String[] args) {
        // 创建主窗口
        JFrame frame = new JFrame("Swing 组件大全");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        
        // 使用选项卡面板组织不同组件
        JTabbedPane tabbedPane = new JTabbedPane();
        
        // 1. 基础组件面板
        JPanel basicPanel = new JPanel(new GridLayout(0, 2, 10, 10));
        basicPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        // 标签
        JLabel label = new JLabel("这是一个标签(JLabel)");
        basicPanel.add(label);
        
        // 按钮
        JButton button = new JButton("按钮(JButton)");
        button.addActionListener(e -> JOptionPane.showMessageDialog(frame, "按钮被点击了!"));
        basicPanel.add(button);
        
        // 文本框
        JTextField textField = new JTextField("单行文本框(JTextField)");
        basicPanel.add(textField);
        
        // 密码框
        JPasswordField passwordField = new JPasswordField("password");
        passwordField.setEchoChar('*');
        basicPanel.add(passwordField);
        
        // 文本区域
        JTextArea textArea = new JTextArea("多行文本区(JTextArea)\n可以输入多行文本");
        textArea.setRows(3);
        basicPanel.add(new JScrollPane(textArea));
        
        // 复选框
        JCheckBox checkBox = new JCheckBox("复选框(JCheckBox)");
        checkBox.addItemListener(e -> {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                System.out.println("复选框被选中");
            } else {
                System.out.println("复选框取消选中");
            }
        });
        basicPanel.add(checkBox);
        
        // 单选按钮
        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
        ButtonGroup radioGroup = new ButtonGroup();
        JRadioButton radio1 = new JRadioButton("选项1", true);
        JRadioButton radio2 = new JRadioButton("选项2");
        JRadioButton radio3 = new JRadioButton("选项3");
        radioGroup.add(radio1);
        radioGroup.add(radio2);
        radioGroup.add(radio3);
        radioPanel.add(radio1);
        radioPanel.add(radio2);
        radioPanel.add(radio3);
        basicPanel.add(radioPanel);
        
        // 下拉框
        String[] options = {"选项A", "选项B", "选项C"};
        JComboBox<String> comboBox = new JComboBox<>(options);
        comboBox.addActionListener(e -> {
            System.out.println("选择了: " + comboBox.getSelectedItem());
        });
        basicPanel.add(comboBox);
        
        // 滑动条
        JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
        slider.setMajorTickSpacing(20);
        slider.setMinorTickSpacing(5);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        basicPanel.add(slider);
        
        // 进度条
        JProgressBar progressBar = new JProgressBar(0, 100);
        progressBar.setValue(40);
        progressBar.setStringPainted(true);
        basicPanel.add(progressBar);
        
        tabbedPane.addTab("基础组件", basicPanel);
        
        // 2. 高级组件面板
        JPanel advancedPanel = new JPanel(new BorderLayout());
        
        // 表格
        String[] columnNames = {"姓名", "年龄", "性别"};
        Object[][] data = {
            {"张三", 25, "男"},
            {"李四", 30, "女"},
            {"王五", 28, "男"}
        };
        JTable table = new JTable(data, columnNames);
        advancedPanel.add(new JScrollPane(table), BorderLayout.CENTER);
        
        // 树形结构
        JPanel treePanel = new JPanel(new GridLayout(1, 2));
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("根节点");
        DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("子节点1");
        DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("子节点2");
        child1.add(new DefaultMutableTreeNode("叶子节点1"));
        child1.add(new DefaultMutableTreeNode("叶子节点2"));
        root.add(child1);
        root.add(child2);
        JTree tree = new JTree(root);
        treePanel.add(new JScrollPane(tree));
        
        // 列表
        String[] listData = {"列表项1", "列表项2", "列表项3", "列表项4"};
        JList<String> list = new JList<>(listData);
        list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        treePanel.add(new JScrollPane(list));
        
        advancedPanel.add(treePanel, BorderLayout.SOUTH);
        tabbedPane.addTab("高级组件", advancedPanel);
        
        // 3. 菜单示例
        JPanel menuPanel = new JPanel();
        menuPanel.setLayout(new BorderLayout());
        
        // 菜单栏
        JMenuBar menuBar = new JMenuBar();
        
        // 文件菜单
        JMenu fileMenu = new JMenu("文件");
        JMenuItem openItem = new JMenuItem("打开");
        JMenuItem saveItem = new JMenuItem("保存");
        JMenuItem exitItem = new JMenuItem("退出");
        exitItem.addActionListener(e -> System.exit(0));
        
        fileMenu.add(openItem);
        fileMenu.add(saveItem);
        fileMenu.addSeparator();
        fileMenu.add(exitItem);
        
        // 编辑菜单
        JMenu editMenu = new JMenu("编辑");
        JMenuItem cutItem = new JMenuItem("剪切");
        JMenuItem copyItem = new JMenuItem("复制");
        JMenuItem pasteItem = new JMenuItem("粘贴");
        
        editMenu.add(cutItem);
        editMenu.add(copyItem);
        editMenu.add(pasteItem);
        
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        
        // 添加到窗口
        frame.setJMenuBar(menuBar);
        menuPanel.add(new JLabel("菜单栏已添加到窗口顶部", SwingConstants.CENTER));
        tabbedPane.addTab("菜单", menuPanel);
        
        // 4. 对话框示例
        JPanel dialogPanel = new JPanel(new GridLayout(2, 2, 10, 10));
        
        JButton messageDialogBtn = new JButton("消息对话框");
        messageDialogBtn.addActionListener(e -> {
            JOptionPane.showMessageDialog(frame, "这是一个消息对话框", "提示", JOptionPane.INFORMATION_MESSAGE);
        });
        dialogPanel.add(messageDialogBtn);
        
        JButton confirmDialogBtn = new JButton("确认对话框");
        confirmDialogBtn.addActionListener(e -> {
            int result = JOptionPane.showConfirmDialog(frame, "您确定要执行此操作吗?", "确认", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                JOptionPane.showMessageDialog(frame, "您选择了是");
            }
        });
        dialogPanel.add(confirmDialogBtn);
        
        JButton inputDialogBtn = new JButton("输入对话框");
        inputDialogBtn.addActionListener(e -> {
            String input = JOptionPane.showInputDialog(frame, "请输入您的名字:");
            if (input != null && !input.isEmpty()) {
                JOptionPane.showMessageDialog(frame, "您好, " + input);
            }
        });
        dialogPanel.add(inputDialogBtn);
        
        JButton optionDialogBtn = new JButton("选项对话框");
        optionDialogBtn.addActionListener(e -> {
            Object[] options2 = {"红色", "绿色", "蓝色"};
            int choice = JOptionPane.showOptionDialog(frame, 
                "请选择您喜欢的颜色", 
                "颜色选择", 
                JOptionPane.DEFAULT_OPTION, 
                JOptionPane.QUESTION_MESSAGE, 
                null,
                    options2,
                    options2[0]);
            if (choice >= 0) {
                JOptionPane.showMessageDialog(frame, "您选择了: " + options2[choice]);
            }
        });
        dialogPanel.add(optionDialogBtn);
        
        tabbedPane.addTab("对话框", dialogPanel);
        
        // 将选项卡面板添加到窗口
        frame.add(tabbedPane);
        
        // 显示窗口
        frame.setVisible(true);
    }
}